package com.demo.autotest.controller;

import com.demo.autotest.common.utils.PageUtils;
import com.demo.autotest.database.domain.CommonParam;
import com.demo.autotest.dto.CommonParamDTO;
import com.demo.autotest.dto.PageDTO;
import com.demo.autotest.request.QueryRequest;
import com.demo.autotest.service.CommonParamService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController // 定义一个controller类
@RequestMapping("/autotest/commonParam")
public class CommonParamController {

    @Resource
    private CommonParamService commonParamService;

    @PostMapping(value = "/save")
    public void saveParam(@RequestBody CommonParam commonParam, HttpServletRequest request){
        String user = request.getSession().getAttribute("userId").toString();
        commonParam.setUpdateUser(user);
        commonParamService.saveParam(commonParam);
    }

    @PostMapping("/delete/{paramId}")
    public void delete(@PathVariable String paramId){
        commonParamService.delete(paramId);
    }

    @PostMapping("/list/{pageNum}/{pageSize}")
    public PageDTO<List<CommonParamDTO>> getCommonParamList(@PathVariable Integer pageNum, @PathVariable Integer pageSize,
                                                     @RequestBody QueryRequest queryRequest){
        Page<Object> page = PageHelper.startPage(pageNum, pageSize, true);
        return PageUtils.setPageInfo(page, commonParamService.getCommonParamList(queryRequest));
    }
}
